package khs.java_mailsend.main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("input host: ");
        String input_host = bufferedReader.readLine();
        
        System.out.print("input port: ");
        String input_port = bufferedReader.readLine();
        
        System.out.print("input auth user: ");
        String input_auth_user = bufferedReader.readLine();
        
        System.out.print("input auth password: ");
        String input_auth_password = bufferedReader.readLine();
        
        System.out.print("input from: ");
        String input_from = bufferedReader.readLine();
        
        System.out.print("input to: ");
        String input_to = bufferedReader.readLine();
        
        System.out.print("input subject: ");
        String input_subject = bufferedReader.readLine();
        
        System.out.print("input body: ");
        String input_body = bufferedReader.readLine();
        
        // Recipient's email ID needs to be mentioned.
        String to = input_to;

        // Sender's email ID needs to be mentioned
        String from = input_from;

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", input_host);
        properties.put("mail.smtp.port", input_port);
        
        final String username = input_auth_user;
        final String password = input_auth_password;

        // Get the default Session object.
        //Session session = Session.getDefaultInstance(properties);
        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(username, password);
                  }
                });

        try{
           // Create a default MimeMessage object.
           MimeMessage message = new MimeMessage(session);

           // Set From: header field of the header.
           message.setFrom(new InternetAddress(from));

           // Set To: header field of the header.
           message.addRecipient(Message.RecipientType.TO,
                                    new InternetAddress(to));

           // Set Subject: header field
           message.setSubject(input_subject);

           // Now set the actual message
           message.setText(input_body);

           // Send message
           Transport.send(message);
           System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
           mex.printStackTrace();
        }
    }
}
